<template>
  <view>
    <ve-confirm titleText="提示" v-model="confirmVisible" @confirm="handleConfirm" @cancel="close" :maskClose="false">
      <view v-for="(it, i) in resultMsg" :key="i">{{ it }}</view>
      <view>{{ confirmTitle }}</view>
    </ve-confirm>
    <ve-toast ref="toastRef" />
  </view>
</template>

<script setup name="PlatoonCarCheckModal">
  import { ref } from 'vue'
  import { platoonCarCheckAPI } from '@/api/order/lOrder'

  // 排车校验
  const toastRef = ref()
  const resultMsg = ref([])
  const confirmVisible = ref(false)
  const confirmTitle = ref('')
  let cancelCbFn = () => {}
  let confirmCbFn = (cb) => {
    cb()
  }
  const confirmFn = async ({ params, title = '是否确认提交？', onOk, onCancel, required = false }) => {
    if (!params.carId) return
    confirmTitle.value = title
    if (onOk) confirmCbFn = onOk
    if (onCancel) cancelCbFn = onCancel
    if (required) confirmVisible.value = true
    toastRef.value.showLoading({ title: '加载中...' })
    platoonCarCheckAPI(params)
      .then((res) => {
        let msgList = res.result.resultMsg || []
        if (msgList.length) {
          resultMsg.value = msgList
          confirmVisible.value = true
        } else {
          if (required == false) handleConfirm()
        }
      })
      .finally(() => {
        toastRef.value.hideLoading()
      })
  }

  const handleConfirm = () => {
    toastRef.value.showLoading({ title: '加载中...' })
    confirmCbFn(() => {
      confirmVisible.value = false
      toastRef.value.hideLoading()
    })
  }

  const close = () => {
    confirmVisible.value = false
    toastRef.value.hideLoading()
    cancelCbFn()
  }

  defineExpose({ confirm: confirmFn })
</script>

<style lang="less" scoped></style>
